import { uploadImage } from "@/api";
import { ElMessage } from "element-plus";

/**
 * 照片上传
 * @param {file} file
 * @returns {string} 照片路径
 */
export async function uploadImageFn(file) {
    console.log(file);
    let fileData = new FormData();
    fileData.append('file', file);
    console.log(fileData.get('file'));
    let res = await uploadImage(fileData)
    if (showMessage(res)) {
        console.log(res);
        return res.data
    }
}

/**
 * 操作成功提示
 * @param {any} data
 * @returns {any}
 */
export function showMessage(res) {
    if (res.code === 200) {
        ElMessage({
            message: res.msg,
            type: 'success',
        })
        return true
    }

    return false

}

/**
 * 表单校验
 * @param {ref} ref
 * @param {Function} callBack
 */
export async function validateFn(ref, callBack) {
    let isValid = await ref.validate()
    console.log(isValid);
    if (isValid) {
        callBack()
    }
}